function toLog() {

    var s = document.getElementById("shade");
    s.style.display = "block";

    var l = document.getElementById("log-func");
    l.style.display = "block";
}
function cancelLog() {

    var s = document.getElementById("shade");
    s.style.display = "none";

    var l = document.getElementById("log-func");
    l.style.display = "none";
}
function toRes() {

    var s = document.getElementById("shade");
    s.style.display = "block";

    var l = document.getElementById("res-func");
    l.style.display = "block";
    var t = document.getElementById("log-func");
    t.style.display = "none";
}
function cancelRes() {

    var s = document.getElementById("shade");
    s.style.display = "none";

    var l = document.getElementById("res-func");
    l.style.display = "none";
}
function openorder() {

    // 先判断登录状态
    var userid = getCookie("userId");
    if (userid == null) {
        alert("您还未登录，请先注册/登录");
        var s = document.getElementById("shade");
        s.style.display = "block";
        var t = document.getElementById("log-func");
        t.style.display = "block";
    }
    else {
        var username = getCookie("userName");
        var carname = $("#cartitle").text();
        $("#selectname").val(carname);
        $("#selectuser").val(username);
        var s = document.getElementById("shade");
        s.style.display = "block";
        var l = document.getElementById("order-func");
        l.style.display = "block";
    }
}
function cancelOrder() {

    var s = document.getElementById("shade");
    s.style.display = "none";

    var l = document.getElementById("order-func");
    l.style.display = "none";
}
// 退出功能
function safeQuit() {

    delCookie("userId");
    delCookie("userName");
    delCookie("is_full");
    window.open("qryCar.html")
}
function commitLog() {

    var userid = $("#userid").val();
    var pwd = $("#pwd").val();
    console.log(userid, pwd);
    $.ajax({
        url : "/DrivingMan/log/login.do",
        type : "post",
        data : {
            "user_Id" : userid,
            "pass_word" : pwd
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                var loginfo = result.data;
                console.log(loginfo);
                var userType = loginfo.userType;
                addCookie("userId", userid, 2);
                addCookie("userType", userType, 2);
                alert(result.detail);
                // 处理返回
                var userInfo = result.data;
                console.log(userInfo);
                var is_full = userInfo.is_full;
                var userName = userInfo.userName;
                addCookie("userName", userName, 2);
                if ("0" == is_full) {// 信息不完善
                    alert("首次登录请先完善个人信息！");
                    addCookie("is_full", is_full, 2);
                    window.location.reload();
                    window.open("personMan.html");
                }
                else {
                    window.location.reload();
                }
            }
            else {
                alert(result.detail);
            }
        },
        error : function() {

            alert("系统异常请稍后再试！");
        }
    });
}
function commitRes() {

    var userid = $("#ruserid").val();
    var pwd = $("#rpwd").val();
    var pwd1 = $("#rpwd1").val();
    console.log(userid, pwd, pwd1);
    if (userid == null || pwd == null || pwd1 == null) {
        alert("用户名或密码不能为空");
        $("#ruserid").val("");
        $("#rpwd").val("");
        $("#rpwd1").val("");
        return;
    }
    if (pwd != pwd1) {
        alert("两次输入密码不一致，请重新输入!");
        $("#ruserid").val("");
        $("#rpwd").val("");
        $("#rpwd1").val("");
        return;
    }
    $.ajax({
        url : "/DrivingMan/log/res.do",
        type : "post",
        data : {
            "userid" : userid,
            "pwd" : pwd
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert(result.detail);
                window.location.reload();
            }
            else {
                alert(result.detail);
                window.location.reload();
            }
        },
        error : function() {

            alert("系统异常请稍后再试！");
        }
    });
}
function qrybyname(a) {

    // 改变样式
    $(".nameon").attr("class", "");
    var $a = $(a);
    $a.attr("class", "nameon");
    var name = $(".nameon").text();
    // 获取其他属性
    var type = $(".typeon").text();
    var price = $(".priceon").text();
    var t = $(".ton").text();
    var ordertype = $(".first").text();
    getCarInfosOrder(name, type, price, t, ordertype);
}
function qrybytype(a) {

    // 改变样式
    $(".typeon").attr("class", "");
    var $a = $(a);
    $a.attr("class", "typeon");
    var type = $(".typeon").text();
    // 获取其他属性
    var name = $(".nameon").text();
    var price = $(".priceon").text();
    var t = $(".ton").text();
    var ordertype = $(".first").text();
    getCarInfosOrder(name, type, price, t, ordertype);
}
function qrybyprice(a) {

    // 改变样式
    $(".priceon").attr("class", "");
    var $a = $(a);
    $a.attr("class", "priceon");
    var price = $(".priceon").text();
    var type = $(".typeon").text();
    var name = $(".nameon").text();
    var t = $(".ton").text();
    var ordertype = $(".first").text();
    getCarInfosOrder(name, type, price, t, ordertype);
}
function qrybyt(a) {

    // 改变样式
    $(".ton").attr("class", "");
    var $a = $(a);
    $a.attr("class", "ton");
    var t = $(".ton").text();
    var price = $(".priceon").text();
    var type = $(".typeon").text();
    var name = $(".nameon").text();
    var ordertype = $(".first").text();
    getCarInfosOrder(name, type, price, t, ordertype);

}

function qryorder(a) {

    $(".first").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "first");
    var price = $(".priceon").text();
    var type = $(".typeon").text();
    var name = $(".nameon").text();
    var ordertype = $(".first").text();
    var t = $(".ton").text();
    getCarInfosOrder(name, type, price, t, ordertype);
}
// 默认查找
function getCarInfo() {

    $
            .ajax({
                url : "/DrivingMan/carInfo/getCarInfo.do",
                type : "post",
                dataType : "json",
                success : function(result) {

                    var carInfos = result.data;

                    for ( var i = 0; i < carInfos.length; i++) {
                        var carinfo = carInfos[i];
                        var carid = carinfo.carid;
                        var img = carinfo.carimg;
                        var carname = carinfo.chexingname;
                        var cartype = carinfo.jibie;
                        var carprice = carinfo.carprice;
                        var li = '<li><div class="list-infoBox"><a href="JavaScript:void(0)">'
                                + '<img width="290" height="194" src='
                                + img
                                + ' alt="'
                                + carname
                                + '" onclick="toCarDetail('
                                + carid
                                + ')"> </a>'
                                + '<p class="infoBox">'
                                + '<a title="" href="JavaScript:void(0)" target="_blank" class="info-title" onclick="toCarDetail('
                                + carid + ')">' + carname + '</a></p>' + '<p class="fc-gray">' + '<span class="">车型：'
                                + cartype + '</span>' + '</p>' + '</p><i class="fc-org priType">' + carprice
                                + '万</i></div></li>';
                        $(".carul").append(li);
                    }
                }
            });
}

function getCarInfosOrder(name, type, price, t, ordertype) {

    $(".carul").find("li").remove();
    $
            .ajax({
                url : "/DrivingMan/carInfo/getCarInfoOrderBy.do",
                type : "post",
                data : {
                    "name" : name,
                    "type" : type,
                    "price" : price,
                    "t" : t,
                    "ordertype" : ordertype
                },
                dataType : "json",
                success : function(result) {

                    var carInfos = result.data;
                    for ( var i = 0; i < carInfos.length; i++) {
                        var carinfo = carInfos[i];
                        var carid = carinfo.carid;
                        var img = carinfo.carimg;
                        var carname = carinfo.chexingname;
                        var cartype = carinfo.jibie;
                        var carprice = carinfo.carprice;
                        var li = '<li><div class="list-infoBox"><a href="JavaScript:void(0)">'
                                + '<img width="290" height="194" src='
                                + img
                                + ' alt="'
                                + carname
                                + '" onclick="toCarDetail('
                                + carid
                                + ')"> </a>'
                                + '<p class="infoBox">'
                                + '<a title="" href="JavaScript:void(0)" target="_blank" class="info-title" onclick="toCarDetail('
                                + carid + ')">' + carname + '</a></p>' + '<p class="fc-gray">' + '<span class="">车型：'
                                + cartype + '</span>' + '</p>' + '</p><i class="fc-org priType">' + carprice
                                + '万</i></div></li>';
                        $(".carul").append(li);
                    }
                }
            });
}

function toCarDetail(carid) {

    window.open("carInfo.html?carid=" + carid + "");
}

function qryCarDetail() {

    var url = window.location.href;
    var carid = url.substring(url.indexOf("carid=") + 6);
    $.ajax({
        url : "/DrivingMan/carInfo/getCarInfoByCarId.do",
        type : "post",
        data : {
            "carid" : carid
        },
        dataType : "json",
        success : function(result) {

            var carInfos = result.data;
            for ( var i = 0; i < carInfos.length; i++) {
                var carinfo = carInfos[i];
                $("#cartitle").text(carinfo.chexingname);
                $("#pinpai").text(carinfo.pinpai);
                $("#cartype").text(carinfo.jibie);
                $("#fadongji").text(carinfo.fadongji);
                $("#biansuxiang").text(carinfo.biansuxiang);
                $("#carckg").text(carinfo.ckg);
                $("#carzj").text(carinfo.zhouju);
                $("#carprice").text("￥" + carinfo.carprice);
                $("#largerimg").attr("src", carinfo.carimg);
                $("#changshang").text(carinfo.pinpai);// FIXME
                $("#jibie").text(carinfo.jibie);
                $("#fadong").text(carinfo.fadongji);//
                $("#biansu").text(carinfo.biansuxiang);//
                $("#cheshenjiegou").text(carinfo.cheshen);
                $("#ckg").text(carinfo.ckg);
                $("#zhouju").text(carinfo.zhouju);
                $("#youxiang").text(carinfo.youxiang);
                $("#sitenum").text(carinfo.zuoweishu);
                $("#pailiang").text(carinfo.pailiang);
                $("#jinqifangshi").text(carinfo.jinqi);
                $("#qigang").text(carinfo.qigang);
                $("#maxml").text(carinfo.maxmali);
                $("#maxnj").text(carinfo.maxnj);
                $("#ranyouleixing").text(carinfo.ranyoutype);
                $("#ranyoubianhao").text(carinfo.ranhoubianhao);
                $("#gongdian").text(carinfo.gongyou);
                $("#paifangbiaozhun").text(carinfo.paifang);
                $("#qudongfangshi").text(carinfo.qudong);
                $("#zhulileixing").text(carinfo.zhuli);
                $("#qianxuangua").text(carinfo.qianxuangua);
                $("#houxuangua").text(carinfo.houxuangua);
                $("#qianzhidong").text(carinfo.qianzhidong);
                $("#houzhidong").text(carinfo.houzhidong);
                $("#zhucheliexing").text(carinfo.zhuchezhidong);
                $("#yuanguangdeng").text(carinfo.yuanguangdeng);
                $("#jinguangdeng").text(carinfo.jinguangdeng);
            }
        }
    });
}

function commitOrder() {

    var userid = getCookie("userId");
    var url = window.location.href;
    var carid = url.substring(url.indexOf("carid=") + 6);
    var selectname = $("#selectname").val();
    var selectuser = $("#selectuser").val();
    var linkphone = $("#linkphone").val();
    var address = $("#Select1").find("option:selected").text() + $("#Select2").find("option:selected").text()
            + $("#Select3").find("option:selected").text() + $("#address").val();
    var starttime = $("#starttime").val();
    var endtime = $("#endtime").val();
    $.ajax({
        url : "/DrivingMan/orderInfo/newOrder.do",
        type : "post",
        data : {
            "carid" : carid,
            "userid" : userid,
            "selectname" : selectname,
            "selectuser" : selectuser,
            "linkphone" : linkphone,
            "address" : address,
            "starttime" : starttime,
            "endtime" : endtime
        },
        dataType : "json",
        success : function(result) {

            if ("0000" == result.status) {
                alert(result.detail);
                cancelOrder();
            }
        }
    });
}

function modifyPwd() {

    var userid = getCookie("userId");
    var oldPwd = $("#oldPwd").val();
    var newPwd = $("#newPwd").val();
    var newPwd1 = $("#newPwd1").val();
    if (newPwd != newPwd1) {
        alert("新密码不一致，请确认！");
        $("#oldPwd").val("");
        $("#newPwd").val("");
        $("#newPwd1").val("");
        return;
    }
    $.ajax({
        url : "/DrivingMan/log/modifyPwd.do",
        type : "post",
        data : {
            "userid" : userid,
            "oldPwd" : oldPwd,
            "newPwd" : newPwd
        },
        dataType : "json",
        success : function(result) {

            alert(result.detail);
            if ("0000" == result.status) {
                close_pwdinfo();
                window.reload(); // 刷新界面
            }
        }
    });

}

function toManPage() {

    var user_type = getCookie("userType");
    if (user_type != "1") {
        alert("对不起，您无权访问，需具有管理员权限才能访问！");
        return;
    }
    else {
        window.open("adminMan.html");
    }
}